<?php
/**
 * CreateTime: 2022/5/3 15:31
 */

namespace App\Func;

use App\Consts\ClientConsts\ClientConsts;
use App\Models\Client\ClientCreditTableModel;
use App\Models\Client\ClientModel;
use App\Models\Client\CreditClientModel;


class ClientFunc
{

    /**
     * 根据客户信用评价表以及客户评价系数等获取客户分值
     * @param $clientType mixed 客户类型
     * @param $creditClientLevel mixed 客户信用级别
     * @param $creditClientNetAssets mixed 客户净资产
     * @param $creditClientRegisteredCapital mixed 客户注册资本
     * @param $enterpriseType mixed 企业类型
     */
    public static function getCreditClientQuotaMax(
        $clientTypeArg,
        $creditClientLevel,
        $creditClientNetAssets,
        $creditClientRegisteredCapital,
        $enterpriseTypeArg
    ){
        // 获取企业类型分类字段
        $enterpriseType = DictionaryFunc::getDictionaryNodeByDictionKey(ClientConsts::ENTERPRISE_TYPE,['dictionary_key','dictionary_value']);
        $formatEnterpriseType = array_column($enterpriseType->toArray(),'dictionary_value','dictionary_key');
        // 获取生产型用户value
        $productionDictionaryValue = $formatEnterpriseType[ClientConsts::ENTERPRISE_TYPE_PRODUCTION];
        // 获取贸易型用户value
        $tradeDictionaryValue = $formatEnterpriseType[ClientConsts::ENTERPRISE_TYPE_TRADE];;
        // 获取最新评价表
        $creditClientTableModel = new ClientCreditTableModel();
        $productionClintTable = $creditClientTableModel->getClientCreditTableByIndustryType($productionDictionaryValue);
        $tradeClintTable = $creditClientTableModel->getClientCreditTableByIndustryType($tradeDictionaryValue);
        // 根据不同类型客户取值
        $clientTypes = DictionaryFunc::getDictionaryNodeByDictionKey(ClientConsts::CLIENT_TYPE,['dictionary_key','dictionary_value']);
        $formatClientType = array_column($clientTypes->toArray(),'dictionary_key','dictionary_value');
        $ruleClientTypeMoney = ClientConsts::CLIENT_TYPE_OTHER_CREDIT_LIMIT_MONEY;
        // 获取A类型客户value
        switch (@$formatClientType[$clientTypeArg]){
            case ClientConsts::CLIENT_TYPE_A:
                $ruleClientTypeMoney = clientConsts::CLIENT_TYPE_A_CREDIT_LIMIT_MONEY;
                if($enterpriseTypeArg == $productionDictionaryValue){
                    $radio = $productionClintTable['a_ratio'];
                }else{
                    $radio = $tradeClintTable['a_ratio'];
                }
                break;
            case ClientConsts::CLIENT_TYPE_B:
                if($enterpriseTypeArg == $productionDictionaryValue){
                    $radio = $productionClintTable['b_ratio'];
                }else{
                    $radio = $tradeClintTable['b_ratio'];
                }
                break;
            case ClientConsts::CLIENT_TYPE_C:
                if($enterpriseTypeArg == $productionDictionaryValue){
                    $radio = $productionClintTable['c_ratio'];
                }else{
                    $radio = $tradeClintTable['c_ratio'];
                }
                break;
          default:
                if($enterpriseTypeArg == $productionDictionaryValue){
                    $radio = $productionClintTable['d_ratio'];
                }else{
                    $radio = $tradeClintTable['d_ratio'];
                }
              break;
        }
        $ruleByAssetsAndRadio = self::getRuleByArgAndRadio($creditClientLevel,$creditClientNetAssets,$radio);
        $ruleByRegisteredCapital = self::getRuleByArgAndRadio($creditClientLevel,$creditClientRegisteredCapital,$radio);
        return self::compare($ruleClientTypeMoney,$ruleByAssetsAndRadio,$ruleByRegisteredCapital);
    }

    /**
     * @param $creditClientLevel
     * @param $assets
     * @param $radio
     * @return float|int
     */
    private static function getRuleByArgAndRadio($creditClientLevel,$arg,$radio): float|int
    {

        return $arg * $radio[$creditClientLevel-1];
    }

    private static function compare($arg1,$arg2,$arg3){
        $arg1 =  app()->gcHelper->gfAutoConvertValueToNumberOrString($arg1);
        $min = $arg1;
        $arg2 = app()->gcHelper->gfAutoConvertValueToNumberOrString($arg2);
        $arg3 = app()->gcHelper->gfAutoConvertValueToNumberOrString($arg3);
        if($min > $arg2){
            $min = $arg2;
        }
        if($min > $arg3){
            $min = $arg3;
        }
        return $min;
    }

    /**
     * 根据信用客户ID获取企业类型
     * @param $creditClientId
     * @return mixed
     */
    public static function getClientIndustryByCreditClientId($creditClientId){
        $cm = new CreditClientModel();
        $clientId = $cm->getCreditClientInfoByCreditClientId($creditClientId,['client_id'])['client_id'];
        $cm = new ClientModel();
        return $cm->getClientInfoByClientId($clientId,['client_industry_type'])['client_industry_type'] ?? 0;
    }

    public static function canUpdateClient($time){
        $delTime = time()-strtotime($time);
        if($delTime>(86400*(365/2))){
            return true;
        }
        return false;
    }




}